const Personal = require("../model/personal");
const formidable = require("formidable"); //处理含有文件上传的表单
const path = require("path");
const uuid = require("uuid"); //产生唯一字符串
const fs = require("fs");
/**
 * 个人日记中间件
 */
module.exports = {
  /**
   *
   * 获取登陆用户个人日记
   */
  getPersinal: (req, res, next) => {
    // 登陆后获取指定用户个人文章
    let { id } = req.query;
    Personal.getPersinal(id)
      .then((res) => {
        req.persinal = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取全部日记
  getPersinalList: (req, res, next) => {
    Personal.getPersinalList()
      .then((res) => {
        req.persinalList = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取日记详情
  getDetails: (req, res, next) => {
    let { id } = req.query;
    Personal.getDetails(id)
      .then((res) => {
        req.details = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取上一篇日记
  getPrev: (req, res, next) => {
    // 获取地址栏传递的id值
    let { id } = req.query;
    Personal.getPrevPersinal(id)
      .then((res) => {
        req.prevPersinal = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取下一篇日记
  getNext: (req, res, next) => {
    // 获取地址栏传递的id值
    let { id } = req.query;
    Personal.getNextPersinal(id)
      .then((res) => {
        req.nextPersinal = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 文章删除
  delete: (req, res, next) => {
    let { id } = req.body;
    Personal.delPersinal(id)
      .then((resolve) => {
        req.affectedRows = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 文章添加
  persinalAdd: (req, res, next) => {
    //处理含有文件上传的表单
    var form = new formidable.IncomingForm();
    //更改上传成功的临时文件的存放位置
    let fullpath = path.resolve(__dirname, "../static/upload");
    form.parse(req, function (err, fields, files) {
      if (!err) {
        // 判断是否有传递过来图片,有则对图片进行重命名
        if (files.description_image_path) {
          //获取文件扩展名
          let extname = path.extname(files.description_image_path.name);
          //完整文件名
          let fullname = uuid.v1() + extname;
          //将上传成功后的临时文件名改成正式文件名
          fs.renameSync(
            files.description_image_path.path,
            fullpath + "/" + fullname
          );
          //向fields对象中添加属性img
          fields.description_image_path = "/upload/" + fullname;
        }
        Personal.persinalAdd(fields)
          .then((resolve) => {
            req.insertId = resolve;
            next();
          })
          .catch((err) => {
            next(err);
          });
      }
    });
  },
   // 日记编辑
   persinalEdit: (req, res, next) => {
    //处理含有文件上传的表单
    var form = new formidable.IncomingForm();
    //更改上传成功的临时文件的存放位置
    let fullpath = path.resolve(__dirname, "../static/upload");
    form.parse(req, function (err, fields, files) {
      if (!err) {
        // 判断是否有传递过来图片,有则对图片进行重命名
        if (files.description_image_path) {
          //获取文件扩展名
          let extname = path.extname(files.description_image_path.name);
          //完整文件名
          let fullname = uuid.v1() + extname;
          //将上传成功后的临时文件名改成正式文件名
          fs.renameSync(
            files.description_image_path.path,
            fullpath + "/" + fullname
          );
          //向fields对象中添加属性img
          fields.description_image_path = "/upload/" + fullname;
        }
        Personal.persinalEdit(fields)
          .then((resolve) => {
            req.insertId = resolve;
            next();
          })
          .catch((err) => {
            next(err);
          });
      }
    });
  },
};
